package com.tongji.seller.view.comp
{
	import com.adobe.images.JPGEncoder;
	import com.tongji.seller.events.AddImageEvent;
	import com.tongji.seller.events.AddStoreSceneEvent;
	import com.tongji.seller.events.GetStoreSceneDetailEvent;
	import com.tongji.seller.model.FurnitureModel;
	import com.tongji.seller.model.GetSystemSceneModel;
	import com.tongji.seller.model.StoreSceneModel;
	import com.tongji.seller.model.UserSceneModel;
	import com.tongji.seller.utils.PopUpAdmin;
	import com.tongji.seller.utils.Showroom;
	import com.tongji.seller.view.Work;
	import com.tongji.seller.view.twoD.Furniture;
	import com.tongji.seller.vo.FurnitureSaved;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.utils.Base64Encoder;
	import mx.utils.OnDemandEventDispatcher;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 *
	 * @author caddy
	 * @date 2011-12-21
	 */
	public class EditSceneCompMediator extends Mediator
	{
		[Inject]
		public var view:EditSceneComp;
		
		[Inject]
		public var storeSceneModel:StoreSceneModel;
		
		[Inject]
		public var furnitureModel:FurnitureModel;
		
		[Inject]
		public var getSystemSceneModel:GetSystemSceneModel;
		
		[Inject]
		public var saveModel:UserSceneModel;
		
		private var angle45Scene:Object;
		private var angle90Scene:Object;
		private var angle135Scene:Object;
		private var selectedScene:Object;
		
		private var angleList:Array;
		public var comp45:Work;
		private var comp135:Work;
		private var comp90:Work;
		
		override public function onRegister():void
		{
			eventMap.mapListener(view.angle45Btn, MouseEvent.CLICK, changeAngleHandler, MouseEvent);
			eventMap.mapListener(view.angle135Btn, MouseEvent.CLICK, changeAngleHandler, MouseEvent);
			eventMap.mapListener(view.angle90Btn, MouseEvent.CLICK, changeAngleHandler, MouseEvent);
			eventMap.mapListener(view.saveUserSceneBtn, MouseEvent.CLICK, saveUserSceneHandler, MouseEvent);
			
//			//保存家具的阴影，亮度，变形8个点的信息
//			eventMap.mapListener(view.saveBtn, MouseEvent.CLICK, SaveFurnitureInforHandler, MouseEvent);
			//还原
			eventMap.mapListener(view.openBtn, MouseEvent.CLICK, openFurnitureInforHandler, MouseEvent);
			
			//createScene();
			addContextListener(GetStoreSceneDetailEvent.UPDATE_SCENE_DETAIL, updateSucHandler, GetStoreSceneDetailEvent);	
			addContextListener(AddImageEvent.UPDATE_IMAGE, addUserSceneHandler, AddImageEvent);
			//view.viewstack.addEventListener(IndexChangedEvent.CHANGE, testHandler);
			//view.angle45.createScene(storeSceneModel.angle45Scene);
			
			/*for (var i:int = 0; i < view.numElements; i++)
			{
				if (view.getElementAt(i) is Work)
				{
					view.removeElementAt(i);
				}
			}*/
			
			view.removeAllElements();
			view.addElement(view.group);
			view.addElement(view.saveUserSceneBtn);

//			view.addElement(view.saveBtn);
			view.addElement(view.openBtn);

			selectedScene = storeSceneModel.sceneDetail;
			comp45 = new Work();
			view.addElementAt(comp45, 0);
			comp45.createScene(storeSceneModel.angle45Scene);
			
			mediatorMap.createMediator(comp45);
			mediatorMap.createMediator(comp45.roomDisplay);
			
			comp135 = new Work();
			view.addElementAt(comp135, 1);
			comp135.visible = false;
			comp135.createScene(storeSceneModel.angle135Scene);
			
			mediatorMap.createMediator(comp135);
			mediatorMap.createMediator(comp135.roomDisplay);
			
			comp90 = new Work();
			view.addElementAt(comp90, 1);
			comp90.visible = false;
			comp90.createScene(storeSceneModel.angle90Scene);
			
			mediatorMap.createMediator(comp90);
			mediatorMap.createMediator(comp90.roomDisplay);
			
			/*var comp135:Work = new Work();
			view.addElementAt(comp135, 1);
			comp135.createScene(storeSceneModel.angle135Scene);
			
			mediatorMap.createMediator(comp135);
			mediatorMap.createMediator(comp135.roomDisplay);*/
			view.getElementAt(0)
		}
		
		
		/**
		 * 场景角度切换 
		 * @param event
		 * 
		 */		
		private function changeAngleHandler(event:MouseEvent):void
		{
			if (event.currentTarget.id == "angle45Btn")
			{				
				comp45.visible = true;
				comp135.visible = false;				
				comp90.visible = false;
			}
			else if (event.currentTarget.id == "angle135Btn")
			{
				comp45.visible = false;
				comp90.visible = false;
				comp135.visible = true;			
			}
			else
			{
				comp45.visible = false;
				comp90.visible = true;
				comp135.visible = false;	
			}		
		}
		
		/**
		 *  
		 * @param event
		 * 
		 */		
		private function updateSucHandler(event:GetStoreSceneDetailEvent):void
		{
			
			
			
			
			/*var comp135:Work = new Work();
			view.addElementAt(comp135, 1);
			comp135.createScene(storeSceneModel.angle135Scene);
			
			mediatorMap.createMediator(comp135);
			mediatorMap.createMediator(comp135.roomDisplay);
			view.viewstack.selectedIndex = 0;
			view.angle45Btn.selected = true;*/
			//mediatorMap.createMediator(view.angle45.roomDisplay);
			selectedScene = storeSceneModel.sceneDetail;
			//view.angle45.createScene(storeSceneModel.angle45Scene);
			for each (var angle:Object in storeSceneModel.sceneDetail.SceneAngleList)
			{
				switch (angle.Jdid)
				{
					case 31:
						angle45Scene = angle;
						//view.angle45.createScene(angle);
						break;
					case 30:
						angle135Scene = angle;
						//view.angle135.createScene(angle);
						break;
					case 35:
						angle90Scene = angle;
						//view.angle90.createScene(angle);
						break;
				}
			}			
		}
		
		/**
		 * 保存场景 
		 * @param event
		 * 
		 */		
		private function addUserSceneHandler(event:AddImageEvent):void
		{			
			saveModel.save(selectedScene, angleList);
		}
		
		private function saveUserSceneHandler(event:MouseEvent):void
		{
			PopUpAdmin.popupCenterWindow(PopUpAdmin.SAVE_LOADING_TITLE_WIN, view.parent, mediatorMap, true);

			var userImages:ArrayCollection = new ArrayCollection();
			angleList = new Array();
			if (comp135 != null)
			{
				userImages.addItem(getImg(comp135));
				angleList.push(saveModel.saveAngleHandler(comp135, storeSceneModel.angle135Scene));
			}
			
			if (comp45 != null)
			{
				userImages.addItem(getImg(comp45));
				angleList.push(saveModel.saveAngleHandler(comp45, storeSceneModel.angle45Scene));
			}
			
			if (comp90 != null)
			{
				userImages.addItem(getImg(comp90));
				angleList.push(saveModel.saveAngleHandler(comp90, storeSceneModel.angle90Scene));
			}
			
			/*if (view.angle45 != null)
			{
				for (var i:int = 0; i < view.angle45.roomDisplay.numChildren; i++)
				{
					var fur:Furniture;
					if (view.angle45.roomDisplay.getChildAt(i) is Furniture)
					{
						fur = view.angle45.roomDisplay.getChildAt(i) as Furniture;
						fur.setDotShow(false);
					}
				}
				userImages.addItem(getImg(view.angle45));
				angleList.push(saveModel.saveAngleHandler(view.angle45, angle45Scene));
			}*/
			    
			
			/*if (view.angle90 != null)
			{
				userImages.addItem(getImg(view.angle90));
				angleList.push(saveModel.saveAngleHandler(view.angle90, angle90Scene));
			}*/
				
			
			dispatch(new AddImageEvent(AddImageEvent.ADD_IMAGES, userImages));
		}
		private function createScene():void
		{
			//view.viewstack.selectedIndex = 0;
			//mediatorMap.createMediator(view.angle45.roomDisplay);
			selectedScene = storeSceneModel.sceneDetail;
			for each (var angle:Object in selectedScene.SceneAngleList)
			{
				switch (angle.Jdid)
				{
					case 31:
						angle45Scene = angle;
						//view.angle45.createScene(angle);
						break;
					case 30:
						angle135Scene = angle;
						//view.angle135.createScene(angle);
						break;
					case 35:
						angle90Scene = angle;
						//view.angle90.createScene(angle);
						break;
				}
			}
			
		}
		
		
		private function getImg(view:Work):String
		{
			var boundingBox:Rectangle = view.borderContainer.getRect(view.borderContainer);
			var matrix:Matrix = new Matrix();
			var bitmapData:BitmapData = new BitmapData(boundingBox.width, boundingBox.height);
			bitmapData.draw(view.borderContainer, matrix);
			
			var encoder:JPGEncoder = new JPGEncoder();
			var byte:ByteArray = encoder.encode(bitmapData);
			var Base64:Base64Encoder = new Base64Encoder();
			Base64.encodeBytes(byte);
			var image:String = Base64.toString();
			return image;			
		}
		
		private function Handler(event:MouseEvent):void
		{
			if (event.currentTarget.id == "angle45Btn")
			{		
				/*view.angle45.createScene(storeSceneModel.angle45Scene);
				mediatorMap.createMediator(view.angle45.roomDisplay);*/
				eventMap.unmapListener(view.angle45Btn, MouseEvent.MOUSE_UP, Handler, MouseEvent);
										
			}
			else if (event.currentTarget.id == "angle135Btn")
			{
				/*mediatorMap.createMediator(view.angle135.roomDisplay);
				view.angle135.createScene(storeSceneModel.angle135Scene);*/
				eventMap.unmapListener(view.angle135Btn, MouseEvent.MOUSE_UP, Handler, MouseEvent);
			}
			else
			{
				/*mediatorMap.createMediator(view.angle90.roomDisplay);
				view.angle90.createScene(storeSceneModel.angle90Scene);		
				eventMap.unmapListener(view.angle90Btn, MouseEvent.MOUSE_UP, Handler, MouseEvent);*/

			}			
		}
		
//		//保存场景中的家具
//		public function SaveFurnitureInforHandler(event:MouseEvent):void
//		{
//			saveFurniture(comp135, 135);
//			saveFurniture(comp45, 45);
//			saveFurniture(comp90, 90);
//		}
		
//		//具体保存的方法
//		private function saveFurniture(work:Work, comp:int):void
//		{
//			trace("work" + work.roomDisplay.numChildren);
//			for (var index:int = 0; index < work.roomDisplay.numChildren; index++)
//			{
//				if (work.roomDisplay.getChildAt(index) is Furniture)
//				{
//					var fursaved:FurnitureSaved = new FurnitureSaved();
//					fursaved.comp = comp;
//					var fur:Furniture = work.roomDisplay.getChildAt(index) as Furniture;
//					fursaved.bitfurcontent = fur.content;
//					var handles:Array = new Array();
//					handles[0] = fur.handle0;
//					handles[1] = fur.handle1;
//					handles[2] = fur.handle2;
//					handles[3] = fur.handle3;
//					handles[4] = fur.handle4;
//					handles[5] = fur.handle5;
//					handles[6] = fur.handle6;
//					handles[7] = fur.handle7;
//					handles[8] = fur.x;
//					handles[9] = fur.y;
//					fursaved.handles = handles;
//					
//					if (fur.filter != null)
//					{
//						fursaved.filter = fur.filter;
//					}
//					fursaved.bright = fur.bitmap.transform.colorTransform.blueOffset;
//					furnitureModel.fursaveds.push(fursaved);
//				}
//			}
//			
//		}
		
		//还原保存的场景中的图片
		private function openFurnitureInforHandler(event:MouseEvent):void
		{
			var t:String = "0,0,223,0,223,219,0,219,111.5,0,223,109.5,111.5,219,0,109.5,0,0,0,0,0,0,0,0,0";
			var chararr:Array = t.split(",");
			
			for (var index:int = 0; index < comp90.roomDisplay.numChildren; index++)
			{
				if (comp90.roomDisplay.getChildAt(index) is Furniture)
				{
					var fur:Furniture = comp90.roomDisplay.getChildAt(index) as Furniture;
					fur.handle0.x = chararr[0];
					fur.handle0.y = chararr[1];
					fur.handle1.x = chararr[2];
					fur.handle1.y = chararr[3];
					fur.handle2.x = chararr[4];
					fur.handle2.y = chararr[5];
					fur.handle3.x = chararr[6];
					fur.handle3.y = chararr[7];
					fur.handle4.x = chararr[8];
					fur.handle4.y = chararr[9];
					fur.handle5.x = chararr[10];
					fur.handle5.y = chararr[11];
					fur.handle6.x = chararr[12];
					fur.handle6.y = chararr[13];
					fur.handle7.x = chararr[14];
					fur.handle7.y = chararr[15];
					fur.filter.distance = chararr[16];
					fur.filter.angle = chararr[17];
					fur.filter.color = chararr[18];
					fur.filter.alpha = chararr[19];
					fur.filter.blurX = chararr[20];
					fur.filter.blurY = chararr[21];
					fur.filter.strength = chararr[22];
					fur.filter.quality = chararr[23];
					fur.bitmap.transform.colorTransform.redOffset = chararr[24];
					fur.bitmap.transform.colorTransform.greenOffset = chararr[24];
					fur.bitmap.transform.colorTransform.blueOffset = chararr[24];
				}
			}
			
//			var fursaveds:Array = furnitureModel.fursaveds;
//			while (fursaveds.length > 0)
//			{
//				var fursaved:FurnitureSaved = fursaveds.shift();
//				var returnfur:Furniture = new Furniture(null, fursaved.bitfurcontent, fursaved.handles);
//				
//				returnfur.scaleX = 0.36;
//				returnfur.scaleY = 0.36;
//				
//				if (fursaved.filter != null)
//				{
//					returnfur.setFilter(fursaved.filter);
//				}
//				returnfur.setBright(new ColorTransform(1, 1, 1, 1, fursaved.bright, fursaved.bright, fursaved.bright, 0));
//				returnfur.x = fursaved.handles[8];
//				returnfur.y = fursaved.handles[9];
//				
//				var work:Work;
//				if (fursaved.comp == 135) 
//				{
//					work = comp135;
//				}
//				if (fursaved.comp == 45) 
//				{
//					work = comp45;
//				}
//				if (fursaved.comp == 90) 
//				{
//					work = comp90;
//				}
//				work.roomDisplay.addChild(returnfur);
//			}
		}
		
	}
}